package com.example.liuhb.androidannotations;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.liuhb.androidannotations.adapter.InfoAdapter;
import com.example.liuhb.androidannotations.entity.MsgInfo;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//1.设置布局
//activity只能继承AppCompatActivity，继承其他activity会报错（待处理）
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
    //2.实例化控件


    @ViewById(R.id.lv_data)
    ListView mLvData;
    @Bean
    InfoAdapter mInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    //3.初始化控件显示
    @AfterViews
    void initViews() {
        // 初始化控件
       // btn.setText("button 按钮");
        mLvData.setAdapter(mInfoAdapter);
    }
    //4.设置控件点击事件
    @Click(R.id.btn)
    void btnClick() {
//        Toast.makeText(MainActivity.this,"aaa",Toast.LENGTH_LONG).show();
        Intent i = new Intent(MainActivity.this,bactivity.class);
        startActivity(i);
        //6.打开新的Activity
        //aaActivity_.intent(this).start(); // 不带参数
        //aaActivity_.intent(this).mInfoStr("this is infoStr.").start(); // 带参数
        //aaActivity_.intent(this).msgInfo(new MsgInfo()).start(); // 带参数
       // finish();
    }
   //7.控件ListView的使用，含adapter（代码较多，请直接看Demo）
    //初始化列表信息
   private List<MsgInfo> getMsgInfo(int size) {
       List<MsgInfo> dataList = new ArrayList<>();
       MsgInfo msgInfo = null;
       for (int i = size; i > 0; i--) {
           msgInfo = new MsgInfo();
           msgInfo.id = String.valueOf(i);
           msgInfo.age = i;
           msgInfo.name = "name_" + i;
           dataList.add(msgInfo);
       }
       return dataList;
   }
   //产生一个不大于30的随机数
    @Click(R.id.btnRefreshList)
    void refreshList() {
        Random random = new Random();
        int size = random.nextInt(30);
        System.out.println(size+"sizesizesizesizesizesizesizesizesizesizesize");
        mInfoAdapter.reloadList(getMsgInfo(size));
    }


}
